package com.himore.api.web.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.himore.api.common.constant.UserConstants;
import com.himore.api.common.core.domain.entity.SysUser;
import com.himore.api.common.utils.DateUtils;
import com.himore.api.common.utils.SecurityUtils;
import com.himore.api.system.mapper.SysUserMapper;
import com.himore.api.web.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.himore.api.web.mapper.TAccountBookMapper;
import com.himore.api.web.service.ITAccountBookService;

/**
 * 账本Service业务层处理
 * 
 * @author dongwang
 * @date 2020-12-15
 */
@Service
public class TAccountBookServiceImpl implements ITAccountBookService 
{
    @Autowired
    private TAccountBookMapper tAccountBookMapper;

    @Autowired
    private SysUserMapper userMapper;
    /**
     * 查询账本
     * 
     * @param accountId 账本ID
     * @return 账本
     */
    @Override
    public TAccountBook selectTAccountBookById(Long accountId){
        return tAccountBookMapper.selectTAccountBookById(accountId);
    }

    /**
     * 查询账本列表
     * 
     * @param tAccountBook 账本
     * @return 账本
     */
    @Override
    public List<TAccountBook> selectTAccountBookList(TAccountBook tAccountBook){
        return tAccountBookMapper.selectTAccountBookList(tAccountBook);
    }

    /**
     * 新增账本
     * 
     * @param tAccountBook 账本
     * @return 结果
     */
    @Override
    public int insertTAccountBook(TAccountBook tAccountBook) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();

        SysUser currUser =userMapper.selectUserById(sysUser.getUserId());
        tAccountBook.setCreateTime(new Date());

        if( ( null != tAccountBook.getIsIncome()  && 0==tAccountBook.getIsIncome()) &&  (UserConstants.OUTCOME_TYPE_FREE.equals(tAccountBook.getPayType())) && null !=tAccountBook.getFreeDays()){
            for(int i=0;i<tAccountBook.getFreeDays();i++){
                TAccountBook book = new TAccountBook();
                book.setPayType(UserConstants.OUTCOME_TYPE_FREE);
                book.setUserId(sysUser.getUserId());
                Date date = tAccountBook.getAccountTime();
                if(i>0){
                    date = DateUtils.addTimeTodate(date,i);
                }
               if(null!= currUser.getBasePay()){
                    int days = DateUtils.getDaysCount(date);
                    //请假的金额按照前台传的金额 计算
                    book.setOutcomeSum(currUser.getBasePay().doubleValue()/days);
                }else {
                    book.setOutcomeSum(0);
                }
                //请假的金额按照前台传的金额 计算
                //book.setOutcomeSum(tAccountBook.getOutcomeSum());
                book.setAccountTime(date);
                book.setCreateTime(new Date());
                book.setPayTypeName(tAccountBook.getPayTypeName());
                book.setIsIncome(tAccountBook.getIsIncome());
                book.setRemark(tAccountBook.getRemark());
                tAccountBookMapper.insertTAccountBook(book);
            }
            return 1;
        }else{
            if("1".equals(tAccountBook.getIncomeType())) {
                if (!"null".equals(tAccountBook.getAccountAmount()) && !"null".equals(tAccountBook.getSinglePrice())) {
                    tAccountBook.setAccountSalary(tAccountBook.getAccountAmount() * tAccountBook.getSinglePrice());
                }
            }
            return tAccountBookMapper.insertTAccountBook(tAccountBook);
        }

    }

    /**
     * 修改账本
     * 
     * @param tAccountBook 账本
     * @return 结果
     */
    @Override
    public int updateTAccountBook(TAccountBook tAccountBook){
        tAccountBook.setUpdateTime(DateUtils.getNowDate());
        if("1".equals(tAccountBook.getIncomeType())){
            if(!"null".equals(tAccountBook.getAccountAmount()) && !"null".equals(tAccountBook.getSinglePrice())){
                tAccountBook.setAccountSalary(tAccountBook.getAccountAmount()* tAccountBook.getSinglePrice());
            }
        }
        return tAccountBookMapper.updateTAccountBook(tAccountBook);
    }

    /**
     * 批量删除账本
     * 
     * @param accountIds 需要删除的账本ID
     * @return 结果
     */
    @Override
    public int deleteTAccountBookByIds(Long[] accountIds){
        return tAccountBookMapper.deleteTAccountBookByIds(accountIds);
    }

    /**
     * 删除账本信息
     * 
     * @param accountId 账本ID
     * @return 结果
     */
    @Override
    public int deleteTAccountBookById(Long accountId){
        return tAccountBookMapper.deleteTAccountBookById(accountId);
    }

    @Override
    public List<TAccountBook> getListByTime(Map param ) {
        return tAccountBookMapper.getListByTime(param);
    }

    @Override
    public TotalInfoVO getMytotalInfo(Map param) {
        return tAccountBookMapper.getMytotalInfo(param);
    }

    @Override
    public List<RecentMonthResultVO> getRecentMonthResult(Map param) {
        return tAccountBookMapper.getRecentMonthResult(param);
    }

    @Override
    public List<DayInfoResultVO> getDayInfoByTime(Map param) {
        return tAccountBookMapper.getDayInfoByTime(param);
    }

    @Override
    public YearCountVO getYearCountResult(Map param) {
        return tAccountBookMapper.getYearCountResult(param);
    }

    @Override
    public List<DayDetailVO> selectTDayInfoList(Map param) {
        return tAccountBookMapper.selectTDayInfoList(param);
    }

    @Override
    public PushServiceInfoVO getPushServiceInfo(Map param) {
        return tAccountBookMapper.getPushServiceInfo(param);
    }


    @Override
    public List<RecentMonthResultVO> getMonthResult(Map param) {
        return tAccountBookMapper.getMonthResult(param);
    }

    /**
     *销假
     *
     * @param param 销假
     * @return 结果
     */
    @Override
    public int  rollbackleaveByParam(Map param){
        return tAccountBookMapper.rollbackleaveByParam(param);
    }

    @Override
    public boolean checkLeaveExists(Map param) {
        int  res =  tAccountBookMapper.checkLeaveExists(param);
        if(res>0){
            return  true;
        }else{
            return  false;
        }
    }

    @Override
    public int deleteTAccountBookByParam(TAccountBook tAccountBook) {
        tAccountBookMapper.deleteTAccountBookByParam(tAccountBook);
        return 1;
    }

    @Override
    public boolean checkSalaryExists(Map param) {
        int  res =  tAccountBookMapper.checkSalaryExists(param);
        if(res>0){
            return  true;
        }else{
            return  false;
        }
    }

    @Override
    public boolean checkIsLast(Map param) {
        int  res =  tAccountBookMapper.checkIsLast(param);
        if(res>0){
            return  false;
        }else{
            return  true;
        }
    }


    /**
     * 新增账本
     *
     * @param tAccountBook 账本
     * @return 结果
     */
    @Override
    public int simpleInsertTAccountBook(TAccountBook tAccountBook) {
        tAccountBook.setCreateTime(new Date());
        return tAccountBookMapper.insertTAccountBook(tAccountBook);
    }



}
